package onlinejudge.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import onlinejudge.constants.Constant;
import onlinejudge.entity.Problem;
import onlinejudge.entity.ProblemLevel;
import onlinejudge.entity.ProblemType;
import onlinejudge.service.NoticeService;
import onlinejudge.service.ProblemService;
import onlinejudge.service.UserProblemService;
import onlinejudge.vo.ProblemVo;
import onlinejudge.vo.UserVo;

@Controller
@RequestMapping("/problem")
public class ProblemController {
	
	@Resource
	private NoticeService noticeService;
	@Resource
	private ProblemService problemService;
	@Resource
	private UserProblemService userProblemService;
	
	/**
	 * 题库页面导向
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		Integer offset=Integer.parseInt(request.getParameter("pager.offset"));
		String queryContent=request.getParameter("queryContent");
		if(queryContent!=null&&queryContent.equals("")) {
			queryContent=null;
		}
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("queryContent",queryContent);
		//再把搜索内容放回页面上,这样保证了对搜索结果的分页
		request.setAttribute("queryContent", queryContent);
		//在未分页之前先获取搜索结果的数量,也便于对搜索结果的分页
		
		UserVo currentUser=(UserVo) request.getSession().getAttribute("currentUser");
		int resSize=0;
		List<ProblemVo> problemList=new ArrayList<ProblemVo>();
		if(currentUser==null) {
			resSize=problemService.queryAllProblem(parameters).size();
			//分页
			parameters.put("page",offset);
			parameters.put("limit",Constant.getRows());
			problemList=problemService.queryAllProblem(parameters);
		}else {
			parameters.put("userId", currentUser.getUserId());
			resSize=userProblemService.queryProblemByUserId(parameters).size();
			//分页
			parameters.put("page",offset);
			parameters.put("limit",Constant.getRows());
			problemList=userProblemService.queryProblemByUserId(parameters);
		}
		
		request.setAttribute("problemList", problemList);
		request.setAttribute("total", resSize);
		request.setAttribute("url", "problem/list");
		
		request.setAttribute("LoginError", request.getParameter("LoginError"));
		request.setAttribute("RegisterError", request.getParameter("RegisterError"));
		request.setAttribute("RegisterSuccess", request.getParameter("RegisterSuccess"));
		request.setAttribute("noticeList", noticeService.queryAllNotice(parameters));
		return "client/problem";
	}
	/**
	 * 获取题目
	 * @param problemId
	 * @param request
	 * @return
	 */
	@RequestMapping("/item")
	public String item(Integer problemId,HttpServletRequest request) {
		UserVo currentUser=(UserVo) request.getSession().getAttribute("currentUser");
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("userId", currentUser.getUserId());
		parameters.put("problemId", problemId);
		request.setAttribute("problem", userProblemService.queryProblemByUserId(parameters).get(0));
		return "client/item";
	}
	/**
	 * 添加题目
	 * @param problem
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Problem problem) {
		problemService.addProblem(problem);
		return "redirect:/admin/addProblem?addFlag=true";
	}
	/**
	 * 返回所有题目接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/problemList")
	public String problemList(int page, int limit) {
		Map<String, Object> parameters1=new HashMap<String, Object>();
		parameters1.put("page", (page-1)*limit);
		parameters1.put("limit", limit);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("count", problemService.getProblemCount());
		json.put("data",problemService.queryAllProblem(parameters1));
		return json.toJSONString();
	}
	/**
	 * 删除题目接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestBody Map<String,Object> map) {
		int problemId=(int) map.get("problemId");
		problemService.deleteProblem(problemId);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 更新题目接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public String update(@RequestBody Map<String,Object> map) {
		int problemId=(int) map.get("problemId");
		String problemTitle=(String) map.get("problemTitle");
		int limitTime=(int) map.get("limitTime");
		int limitSpace=(int) map.get("limitSpace");
		String problemSource=(String) map.get("problemSource");
		Problem problem=new Problem();
		problem.setProblemId(problemId);
		problem.setProblemTitle(problemTitle);
		problem.setLimitTime(limitTime);
		problem.setLimitSpace(limitSpace);
		problem.setProblemSource(problemSource);
		problemService.updateProblem(problem);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 返回所有题目难度接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/problemLevelList")
	public String problemLevelList(int page, int limit) {
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("page", (page-1)*limit);
		parameters.put("limit", limit);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("count",problemService.getProblemLevelCount());
		json.put("data",problemService.queryAllProblemLevel(parameters));
		return json.toJSONString();
	}
	/**
	 * 返回所有题目类型接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/problemTypeList")
	public String problemTypeList(int page, int limit) {
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("page", (page-1)*limit);
		parameters.put("limit", limit);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("count",problemService.getProblemTypeCount());
		json.put("data",problemService.queryAllProblemType(parameters));
		return json.toJSONString();
	}
	/**
	 * 添加题目难度接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addProblemLevel")
	public String addProblemLevel(@RequestBody Map<String,Object> map) {
		String problemLevelName=(String) map.get("problemLevelName");
		ProblemLevel problemLevel=new ProblemLevel();
		problemLevel.setProblemLevelName(problemLevelName);
		problemService.addProblemLevel(problemLevel);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 修改题目难度接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateProblemLevel")
	public String updateProblemLevel(@RequestBody Map<String,Object> map) {
		Integer problemLevelId=(Integer) map.get("problemLevelId");
		String problemLevelName=(String) map.get("problemLevelName");
		ProblemLevel problemLevel=new ProblemLevel(problemLevelId, problemLevelName);
		problemService.updateProblemLevel(problemLevel);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 删除题目难度接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteProblemLevel")
	public String deleteProblemLevel(@RequestBody Map<String,Object> map) {
		Integer problemLevelId=(Integer) map.get("problemLevelId");
		problemService.deleteProblemLevel(problemLevelId);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 添加题目类型接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addProblemType")
	public String addProblemType(@RequestBody Map<String,Object> map) {
		String problemTypeName=(String) map.get("problemTypeName");
		ProblemType problemType=new ProblemType();
		problemType.setProblemTypeName(problemTypeName);
		problemService.addProblemType(problemType);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 修改题目类型接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateProblemType")
	public String updateProblemType(@RequestBody Map<String,Object> map) {
		Integer problemTypeId=(Integer) map.get("problemTypeId");
		String problemTypeName=(String) map.get("problemTypeName");
		ProblemType problemType=new ProblemType(problemTypeId, problemTypeName);
		problemService.updateProblemType(problemType);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 删除题目类型接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteProblemType")
	public String deleteProblemType(@RequestBody Map<String,Object> map) {
		Integer problemTypeId=(Integer) map.get("problemTypeId");
		problemService.deleteProblemType(problemTypeId);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data","");
		return json.toJSONString();
	}
	/**
	 * 查看已做对题目的代码
	 * @param userId
	 * @param problemId
	 * @param request
	 * @return
	 */
	@RequestMapping("/successItem")
	public String successItem(Integer userId,Integer problemId,HttpServletRequest request) {
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("userId", userId);
		parameters.put("problemId", problemId);
		request.setAttribute("problem", userProblemService.getSuccessProblem(userId, problemId));
		return "client/successItem";
	}
}
